<?php
/**
 * Created by PhpStorm.
 * User: 73640
 * Date: 2018/10/18
 * Time: 15:44
 */

namespace App\Repository;


use App\model\Building;

class BuildingRepository
{
    /**
     * 园区列表
     *
     * @param  $request
     * city_id 城市
     * city_first_id 二级城市id
     * city_second_id 三级
     * area_id
     * sreach_key
     * 筛选条件
     * page_id 页码
     * 排序条件
     * sort_type  字段
     * 排序字段
     * sort 升序 降序
     *
     *link_model industry,propertyType,building
     */
    public function getList($request){
         $building=Building::with(['park','broker','areaBuild']);

        if(isset($request['city_id'])&&$request['city_id']!=0){
            $building=$building->where('building.p_province_id',$request['city_id']);
        }
        if(isset($request['area_id'])&&$request['area_id']!=0){
            $building=$building->where('building.p_city_id',$request['area_id']);
        }
        if(isset($request['area1_id'])&&$request['area1_id']!=0){
            $building=$building->where('building.p_district_id',$request['area1_id']);
        }
        if(isset($request['search_key'])&&$request['search_key']!=''){
            $building=$building->where('building.title','like','%'.$request['search_key'].'%');
        }
         if(isset($request['area_build_id'])&&$request['area_build_id']!=0){
             $building=$building->where('building.area_build_id',$request['area_build_id']);
         }
         if(isset($request['park_id'])&&$request['park_id']!=0){
             $building=$building->where('building.park_id',$request['park_id']);
         }
        if(isset($request['is_company'])&&$request['is_company']!=0){
            $building=$building->where('building.is_company',$request['is_company']);
        }
        if(isset($request['is_separate'])&&$request['is_separate']!=0){
            $building=$building->where('building.is_separate',$request['is_separate']);
        }
        if(isset($request['renovation'])&&$request['renovation']!=0){
            $building=$building->where('building.renovation',$request['renovation']);
        }
        if(isset($request['is_owner'])&&$request['is_owner']!=0){
            $building=$building->where('building.is_owner',$request['is_owner']);
        }
        if(isset($request['is_recommend'])&&$request['is_recommend']!=''){
            $building=$building->where('is_recommend',$request['is_recommend']);
        }
        if(isset($request['sort_type'])&&$request['sort_type']!=''){
            $building=$building->orderBy('building.'.$request['sort_type'],$request['sort'] ?? 'asc');
        }else{
            $building=$building->orderBy('building.id','desc');
        }
        /*if(isset($request['is_recommend'])&&$request['is_recommend']!=''){
            $building=$building->where('is_recommend',$request['is_recommend']);
        }
        if(isset($request['link_model'])&&$request['link_model']!=''){
            $link_model = $request['link_model'];
            if(strpos($link_model,',')){
                $link_model = explode(',',$link_model);
            }else if(strpos($link_model,'，')){
                $link_model = explode('，',$link_model);
            }
            $building=$building->with($link_model);
        }*/
       $building=$building->select('building.*');
       $pagesize=$request['pagesize'] ?? 10;
       $building= $building->paginate($pagesize);
       return $building;
    }

    /**
     * @param int $workshop_id
     */
    public function detail(int $building_id){
        $building = Building::with(['areaBuild','broker','park'=>function($query){
            $query->with(['parkType','parkFormat']);
        }])->where('id',$building_id)->firstOrFail()->toArray();
        return $building;
    }

}